<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="生产编号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入生产编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="生产日期" prop="produceDate">
        <el-date-picker clearable v-model="queryParams.produceDate"
                        type="date" value-format="yyyy-MM-dd" placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="提交日期" prop="submitDate">
        <el-date-picker clearable v-model="queryParams.submitDate"
                        type="date" value-format="yyyy-MM-dd" placeholder="请选择提交日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['develop:ProduceRecord:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['develop:ProduceRecord:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-popconfirm title="确认删除选择的数据项？" @confirm="handleDelete({})">
          <el-button slot="reference" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                     v-hasPermi="['develop:ProduceRecord:remove']">删除
          </el-button>
        </el-popconfirm>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['develop:ProduceRecord:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="生产编号" align="center" prop="nos"/>
      <el-table-column label="生产日期" align="center" prop="produceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交日期" align="center" prop="submitDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.produce_record_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="退回原因" align="center" prop="refuseReason"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-popconfirm :title="`确认提交质检当前数据项？`" @confirm="handleSubmit(scope.row)"
                         v-if="queryParams.status === '0'|| queryParams.status === '3' " style="margin: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-check"
                       v-hasPermi="['develop:ProduceRecord:edit']">提交质检
            </el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认质检弃审当前数据项？`" @confirm="handleInspectReject(scope.row)"
                         v-if="queryParams.status === '1' || queryParams.status === '5'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-error"
                       v-hasPermi="['develop:ProduceRecord:inspect']">质检弃审
            </el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认质检通过当前数据项？`" @confirm="handleInspectPass(scope.row)"
                         v-if="queryParams.status === '1' || queryParams.status === '5'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-success"
                       v-hasPermi="['develop:ProduceRecord:inspect']">质检通过
            </el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认库管入库当前数据项？`" @confirm="handleStorePass(scope.row)"
                         v-if="queryParams.status === '2'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-check"
                       v-hasPermi="['develop:ProduceRecord:store']">库管入库
            </el-button>
          </el-popconfirm>

          <el-button size="mini" type="text" icon="el-icon-circle-close" v-show="queryParams.status === '2'"
                     @click="handleStoreReject(scope.row)" v-hasPermi="['develop:ProduceRecord:store']">库管弃审
          </el-button>

          <el-button  size="mini" type="text" icon="el-icon-circle-close" v-show="queryParams.status === '4'"
                     @click="storeGoodsReturn(scope.row)" v-hasPermi="['develop:ProduceRecord:store']">入库退货
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-document" style="margin-left: 10px;"
                     @click="showDetail(scope.row)">生产单明细
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" v-if="queryParams.status === '0' || queryParams.status === '3' || queryParams.status === '5'"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:ProduceRecord:edit']">修改
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                         v-if="queryParams.status === '0' || queryParams.status === '3' || queryParams.status === '5'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:ProduceRecord:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <produce-record-refuse-form ref="refuseForm" @ok="getList"></produce-record-refuse-form>
    <produce-record-refuse-store-form ref="refuseStoreForm" @ok="getList"></produce-record-refuse-store-form>
    <produceRecord-detail-list-form ref="detailListForm" @ok="getList"></produceRecord-detail-list-form>
    <produce-record-detail-list-module ref="produceRecordDetailListModule" @ok="getList"></produce-record-detail-list-module>
  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import ProduceRecordDetailListForm from '@/views/develop/produceRecord/module/ProduceRecordDetailListForm'
import ProduceRecordDetailListModule
  from "@/views/develop/produceRecordDetail/module/ProduceRecordDetailListModule.vue";
import ProduceRecordRefuseForm from "@/views/develop/produceRecord/module/ProduceRecordRefuseForm.vue";
import ProduceRecordRefuseStoreForm from "@/views/develop/produceRecord/module/ProduceRecordRefuseStoreForm.vue";

export default {
  name: "ProduceRecordList",
  dicts:["produce_record_status"],
  components: {
    ProduceRecordRefuseStoreForm,
    ProduceRecordRefuseForm,
    ProduceRecordDetailListModule,
    ProduceRecordDetailListForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产记录表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nos: null,
        produceDate: null,
        submitDate: null,
        status: null,
        orderByColumn: 'a.nos',
        isAsc: "desc"
      },
      url: {
        list: '/produceRecord/produceRecord/list',
        remove: '/produceRecord/produceRecord/',
        export: '/produceRecord/produceRecord/export',
        submit: '/produceRecord/produceRecord/submit/',
        inspectPass: '/produceRecord/produceRecord/inspectPass/',
        inspectReject: '/produceRecord/produceRecord/inspectReject/',
        storePass: '/produceRecord/produceRecord/storePass/',
        storeReject: '/produceRecord/produceRecord/storeReject',
        storeReturn: '/produceRecord/produceRecord/storeReturn',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.queryParams.status = this.$route.query.status;
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询生产记录列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.detailListForm.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.detailListForm.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `produceRecord_${new Date().getTime()}.xlsx`)
    },

    showDetail(row) {
      this.$refs.produceRecordDetailListModule.showDetail(row);
    },


    handleSubmit(row){
      getAction(this.url.submit + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleInspectPass(row){
      getAction(this.url.inspectPass + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleInspectReject(row){
      getAction(this.url.inspectReject + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleStorePass(row){
      getAction(this.url.storePass + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleStoreReject(row){
      this.$refs.refuseForm.handleRefuse(row);
      // getAction(this.url.storeReject + "/" + row.id, {}).then(response => {
      //   this.$modal.msgSuccess("操作成功");
      //   this.getList();
      // }).catch(() => {
      // });
    },

    storeGoodsReturn(row){
      this.$refs.refuseStoreForm.handleRefuse(row);
      // getAction(this.url.storeReturn + "/" + row.id, {}).then(response => {
      //   this.$modal.msgSuccess("操作成功");
      //   this.getList();
      // }).catch(() => {
      // });
    },








  }
};
</script>

